/*
* Copyright 2015 2dfire.com All right reserved. This software is the
* confidential and proprietary information of 2dfire.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with 2dfire.com.
*/
package com.dfire.mtm;

import com.dfire.mtm.executor.AbstractMtmRunnable;
import com.dfire.mtm.executor.MtmRunnable;
import com.dfire.mtm.task.TaskManager;
import com.dfire.mtm.task.impl.DefaultTaskManager;
import com.dfire.mtm.taskNode.TaskData;
import com.dfire.mtm.taskNode.TaskNode;
import com.dfire.mtm.taskNode.impl.BlockTaskNode;
import com.dfire.mtm.taskNode.impl.DefaultTaskNode;
import com.google.common.collect.Lists;

import java.util.concurrent.Executors;

/**
 * @author guipi
 * @description
 * @date 2018/10/23
 * ----------重大变更记录------------
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        MtmRunnable startRunnable = new AbstractMtmRunnable() {
            @Override
            public void run() {
                TaskData taskData = getData();
//                System.out.println("Current Thread:"+Thread.currentThread().getName());
                if(taskData==null){
                    return;
                }
                System.out.println("this is "+getTaskNode().getName()+",get data:"+taskData.getT());
                pushData(taskData.getTask(),"1->2 "+taskData.getT());
            }
        };

        TaskNode startNode = new DefaultTaskNode("startNode");
        startNode.setRunnable(startRunnable);
        startNode.setExecutor(Executors.newSingleThreadExecutor());

        MtmRunnable secondRunnable = new AbstractMtmRunnable() {
            @Override
            public void run() {
                TaskData taskData = getData();
//                System.out.println("Current Thread:"+Thread.currentThread().getName());
                if(taskData==null){
                    return;
                }
                System.out.println("this is "+getTaskNode().getName()+",get data:"+taskData.getT());
                pushData(taskData.getTask(),"threeNode","2->3 " +taskData.getT());
                pushData(taskData.getTask(),"fourNode","2->4 "+taskData.getT());
            }
        };

        TaskNode secondNode = new DefaultTaskNode("secondNode",secondRunnable,Executors.newFixedThreadPool(2));
        startNode.addChildrenNode(Lists.newArrayList(secondNode));


        MtmRunnable threeRunnable = new AbstractMtmRunnable() {
            @Override
            public void run() {
                TaskData taskData = getData();
//                System.out.println("Current Thread:"+Thread.currentThread().getName());
                if(taskData==null){
                    return;
                }
                System.out.println("this is "+getTaskNode().getName()+",get data:"+taskData.getT());
                pushData(taskData.getTask(),"fiveNode","3->5 "+taskData.getT());
            }
        };

        TaskNode threeNode = new DefaultTaskNode("threeNode");
        threeNode.setRunnable(threeRunnable);
        threeNode.setExecutor(Executors.newFixedThreadPool(2));

        MtmRunnable fourRunnable = new AbstractMtmRunnable() {
            @Override
            public void run() {
                TaskData taskData = getData();
//                System.out.println("Current Thread:"+Thread.currentThread().getName());
                if(taskData==null){
                    return;
                }
                System.out.println("this is "+getTaskNode().getName()+",get data:"+taskData.getT());
                pushData(taskData.getTask(),"fiveNode","4->5 "+taskData.getT());
            }
        };
        TaskNode fourNode = new DefaultTaskNode("fourNode");
        fourNode.setRunnable(fourRunnable);
        fourNode.setExecutor(Executors.newFixedThreadPool(2));
        secondNode.addChildrenNode(Lists.newArrayList(threeNode,fourNode));

        MtmRunnable fiveRunnable = new AbstractMtmRunnable() {
            @Override
            public void run() {
                TaskData taskData = getData();
//                System.out.println("Current Thread:"+Thread.currentThread().getName());
                if(taskData==null){
                    return;
                }
                System.out.println("this is "+getTaskNode().getName()+",get data:"+taskData.getT());
            }
        };

        TaskNode fiveNode = new BlockTaskNode("fiveNode",fiveRunnable,Executors.newFixedThreadPool(1));
        threeNode.addChildrenNode(Lists.newArrayList(fiveNode));
        fourNode.addChildrenNode(Lists.newArrayList(fiveNode));

        TaskManager task = new DefaultTaskManager(startNode);

        task.start();

        for(int i =0;i<5;i++){
            task.produce("task1",i);
        }
        task.notifyTaskFinish("task1");

        for(int i =65;i<70;i++){
            task.produce("task2",(char)i);
        }
        task.notifyTaskFinish("task2");
        //task.stop();
    }
}
